VBScript → Lua
The following function fills an array with the names of a files in a given folder. The names contain the complete path.
It can be used for example, to get a list of announcement (.wav) files in a folder to play them one after each other. See example code below...
Please see the Introduction chapter for some usage instructions.
'--------------------------------------------------------------
' GetFilesInFolder()
'
' Reads all file names in given folder into a given array
'
' Parameter:
' Path [in] name of path
' FilesArray [out] array to store all filenames in
'
' Return:
' boolean TRUE - files found
' FALSE - no files found
'--------------------------------------------------------------
Function GetFilesInFolder ( Path, ByRef FilesArray )
PBXScript.OutputTrace "-----------> GetFilesInFolder( " & Path & " )"
Dim bReturn
bReturn = False
Dim fso, folder, files, file
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(Path)
Set files = folder.files
Redim FilesArray(0)
If files.count <> 0 Then
bReturn = True
For Each file In files
Redim Preserve FilesArray(UBound(FilesArray)+1)
FilesArray(UBound(FilesArray)) = file.path
PBXScript.OutputTrace CStr(UBound(FilesArray)) & ": " & FilesArray(UBound(FilesArray))
Next
End If
Set fso = Nothing
GetFilesInFolder = bReturn
PBXScript.OutputTrace "bReturn = " & bReturn
PBXScript.OutputTrace "<----------- GetFilesInFolder"
End Function
This function makes use of the Server Script API function PBXScript.OutputTrace to write trace information into the SwyxServer trace file.
The following example code, which could be placed into an Insert Script Code block play all .wav files in a given folder:
Dim Announcements()
Dim i
If GetFilesInFolder("C:\Announcements\", Announcements) Then
For i = LBound(Announcements) to UBound(Announcements)
PBXCall.PlayMessage Announcements(i)
Next
End If
This exmaple makes use of the Server Script API function PBXCall.PlayMessage function to play a given .wav file.
The .wav files need to be in 16kHz, 16bit, PCM, mono format.
This function and the example was initially posted in this forum topic.
By Tom Wellige
